
var that = this;
var typeList = [
    {title: "代金券", id: "1",name:"CASH"},
    {title: "折扣券", id: "2",name:"DISCOUNT"},
    {title: "兑换券", id: "3",name:"GIFT"},
    {title: "优惠券", id: "4",name:"GENERAL_COUPON"},
    {title: "团购券", id: "5",name:"GROUPON"},
];
var colorList = [
    {color: "#63b359"},
    {color: "#2c9f67"},
    {color: "#509fc9"},
    {color: "#5885cf"},
    {color: "#9062c0"},
    {color: "#d09a45"},
    {color: "#e4b138"},
    {color: "#ee903c"},
    {color: "#f08500"},
    {color: "#a9d92d"},
    {color: "#dd6549"},
    {color: "#cc463d"},
    {color: "#cf3e36"},
    {color: "#5E6671"}
];
var app = new Vue({
    el: '#app_cardus',
    data: {
        isActive: "CASH",
        mid: "",
        tianlist: [],
        num: 0,
        logopic: "",
        brand_name: "",
        card_name: "卡券名称",
        get_limit : 1,
        use_limit: 1,
        create_mid:"",
        service_phone:"",
        card: "",//当前卡卷
        mchlist: "",
        active: 0,
        mkey:0,
        typeList: typeList,
        colorList:colorList,
        color:"#63b359",
        iscolor:"#63b359",
        titletext: 100,
        title: "",
        hurry_wrap: true,
        yh_money: true,
        yh_discount: false,
        yh_jieshao: false,
        subhurry_wrap:false,
        type:1,
        type_name: "代金券详情",
        imgnum: 1,
        abstract: "查看更多",
        imglist: [
            {id: ''}
        ],
        isdelimg: false,
        isaddimg: true,
        fixed_begin_term: 0,

    },
    mounted: function () {
        this.getMenu();

        this.gettiannum();
    },

    methods: {
        changeSwitch(checked, id, index) {
            // console.log(index)
        },
        selemch:function(){

            // console.log(this.mkey)
            this.mid=this.mchlist[this.mkey].id;
            this.brand_name=this.mchlist[this.mkey].merchant;
        },
        colortype:function(index, cid){
         // console.log(cid);
         this.color=cid
            this.iscolor = cid;
        },
        tabstype: function (index,name) {
            this.isActive = name;
            this.type = name;
            if (name == 'CASH') {
                this.hurry_wrap = true;
                this.yh_money = true;
                this.yh_jieshao = false;
                this.type_name = "代金券详情"

            } else if (name == "DISCOUNT") {
                this.hurry_wrap = false;
                this.yh_money = false;
                this.yh_discount = true;
                this.type_name = "折扣券详情"


            } else if (name == "GIFT") {
                this.hurry_wrap = false;
                this.yh_money = false;
                this.yh_jieshao = true;
                this.type_name = "兑换券详情";

            } else if (name == "GENERAL_COUPON") {
                this.hurry_wrap = false;
                this.yh_money = false;
                this.yh_jieshao = true;
                this.type_name = "优惠券详情";

            } else if (name == "GROUPON") {
                this.hurry_wrap = false;
                this.yh_money = false;
                this.yh_jieshao = true;
                this.type_name = "团购券详情";

            }
        },
        gettiannum: function () {
            var tian = 92;

            for (var i = 1; i < tian; i++) {

                this.tianlist.push(i)
            }
        },
        getMenu: function () {
            var _this = this
            $.ajax({
                url: getcardusAPI,
                type: "POST",
                dataType: "json",
                data:{
                    method:"getinfo"
                },
                async: false,
                success: function (res) {
                    // console.log(res);
                    if (res.return_code == "SUCCESS") {
                        _this.card = res.info;
                        _this.service_phone=res.info.base_info.service_phone;
                        _this.mid=res.info.mid;
                        _this.logopic = res.info.logo_url;
                        _this.brand_name = res.info.brand_name;
                        _this.create_mid=res.info.create_mid;
// console.log(12,_this.logopic)
                    }
                }
            })
        },
        methodGetByteLen(str, len) {
            for (let i = Math.floor(len / 2); i < str.length; i++) {
                if (str.substr(0, i).replace(/[^\x00-\xff]/g, '01').length >= len) {
                    return str.substr(0, Math.floor(i / 2) * 2)
                }
            }
        },
        // 控制显示的内容
        settitle() {
            var that = this;
            var thtext = that.title;
            that.title = thtext
            that.card_name = thtext
            // console.log(thtext);

            return thtext

        },
        setget_limit(){
            var that = this;
            var ths = that.get_limit;
            that.get_limit = ths
            that.use_limit = ths
            // console.log(thtext);

            return ths
        },

        //添加图文上传div
        addNode: function () {
            var that = this;
            that.imgnum++;

            that.imglist.push({id: ''});
            if (that.imgnum >= 2 && that.imgnum > 1) {
                that.isdelimg = true
            }
            if (that.imgnum >= 5) {
                that.isaddimg = false
            }
            // console.log(imgnum)
        },
        //删除图文上传div
        deleteNode: function (i) {
            this.imglist.splice(i, 1);  //删除index为i,位置的数组元素
        },


    }
})


// 检查字数
function tex(idq, tex, num) {
    //console.log(idq,tex,num)
    $("#" + tex).text("还可以输入" + (num - $("#" + idq).val().length) + "个字");


    if ($("#" + idq).val().length > num) {
        $("#" + idq).val($("#" + idq).val().substring(0, num));
    }
    $("#" + tex).text("还可以输入" + (num - $("#" + idq).val().length) + "个字");
    var val = $("#" + idq).val();
    //console.log(11,val);
    $("." + idq).html(val)
    $("." + idq).val(val)
}

// 展开隐藏
$(".areaShowHide").click(function () {

    if ($(".card_info_box").is(":hidden")) {
        $(".card_info_box").fadeIn();
        $(".areaShowHide").html('<span>收起</span> <i class=" glyphicon glyphicon-chevron-down"></i>');
    } else {
        $(".areaShowHide").html('<span>展开</span> <i class=" glyphicon glyphicon-chevron-up"></i>');
        $(".card_info_box").fadeOut();
    }
    closeTip($(".card_info_box input"));
});

function closeTip(element) {
    var tips_index = $(element).attr("tip-idx");
    layer.close(tips_index);
    $(element).attr("tip-idx", "");
    $(element).addClass("valid");
}

// 选择时间
function settimes(obj, name) {

    var val = $('input[name="date_info"]:checked').val();

    if (val == 'DATE_TYPE_FIX_TIME_RANGE') {
        $("#fixed_begin_term").attr("disabled", true);
        $("#fixed_term").attr("disabled", true);
        $("#end_timestamp").attr("disabled", true);

        $("#starttime").attr("disabled", false);

    } else if (val == 'DATE_TYPE_FIX_TERM') {
        $("#starttime").attr("disabled", true);
        $("#fixed_begin_term").attr("disabled", false);
        $("#fixed_term").attr("disabled", false);
        $("#end_timestamp").attr("disabled", false);
    }
}
$("#share").change(function () {

    if ($(this).is(":checked")) {

        $("#can_share").val(1);
    } else {

        $("#can_share").val(0);
    }
});
$("#give").change(function () {

    if ($(this).is(":checked")) {

        $("#can_give_friend").val(1);
        $("#quick_pay").removeClass("hide");
    } else {
        $("#can_give_friend").val(0);
        $("#quick_pay").addClass("hide");
    }
});
$("#pay").change(function () {

    if ($(this).is(":checked")) {

        $("#is_pay").val(1);
    } else {

        $("#is_pay").val(0);
    }
});
$("#cgive").change(function () {

    if ($(this).is(":checked")) {

        $("#is_cgive").val(1);
    } else {

        $("#is_cgive").val(0);
    }
});
$("#hurry").on('switch-change', function (e, data) {
    var $el = $(data.el), value = data.value;
    if (value == true) {
        $("#can_hurry").val(1);
        $("#dis").prop("disabled", "true");
        app.subhurry_wrap=true;
    } else {
        $("#can_hurry").val(0);
        $("#dis").removeAttr("disabled");
        app.subhurry_wrap=false;
    }
});
$(window).scroll(function () {
    if ($(window).scrollTop() > 106) {
        $(".card-left").css({"position": "fixed", "top": "106px"});
    } else {
        $(".card-left").css({"position": "absolute", "top": ""});
    }
});
// 图片删除
$(".upload-v-img").click(function () {

    $("#fimg").attr("style", "display:none");
    $(".img").attr("src", "");
    $(".fengmian_img_parent").addClass("hide");
})

// 图片删除
$(".brand_item").delegate('.imgTxtDel', "click", function () {
    var $parent = $(this).closest(".imagetxts");
    // console.log($parent);
    imgRemoveStyle($parent);
});

// 图片删除效果
function imgRemoveStyle($parent) {
    $parent.find(".imageShow").attr("src", "");
    $parent.find(".imageShow").parent().hide();
    $parent.find(".wechatImage").val("");
    $parent.find(".image").val("");
    $parent.find(".imageTxtFile").val("");
    $parent.find(".upImage").show();
}

$("#creatbtn").click(function () {

    var mid = $("#mid").val();
    var kqmc = $("#kqmc").val();
    var create_mid = $("#create_mid").val();
    var get_limit = $("#get_limit").val();
    var use_limit = $("#use_limit").val();
    var quantity = $("#quantity").val();
    var notice = $("#notice").val();
    var description = $("#description").val();
    var service_phone = $("#service_phone").val();
    var starttime = $("#starttime").val();
    var start = $("#beg_time").val();
    var end = $("#end_time").val();
    var cover =  $('input[name="date_info"]:checked').val();
    var fixed_term = $('#fixed_term').val();
    var logo_url = $("#logo_url").val();
    var brand_name = $("#brand_name").val();
    var yh = app.yh_discount;
    var money = app.yh_money;
    var type = app.yh_discount;
    var jieshao = app.yh_jieshao;
    var wrap = app.hurry_wrap;

// console.log(yh);
    if (mid == "") {
        layer.alert("请选择门店！");
        return false;
    }
    if (kqmc == "") {
        layer.alert("请填写卡券名称！");
        return false;
    }

    if (wrap == true) {
        if (create_mid == "") {
            layer.alert("请填写商户号！");
            return false;
        }
    }
    if (get_limit == "") {
        layer.alert("请填写领取限制！");
        return false;
    }
    if (use_limit == "") {
        layer.alert("请填写使用限制！");
        return false;
    }
    if (cover == "DATE_TYPE_FIX_TIME_RANGE") {

        if (starttime == "") {
            layer.alert("请选择开始时间");
            return false;
        }

    } else if (fixed_term == "") {
        layer.alert("请选择天数");
        return false;
    }
    if (quantity == "") {
        layer.alert("请填写库存！");
        return false;
    }


    if (notice == "") {
        layer.alert("请填写使用提醒！");
        return false;
    }

    if (description == "") {
        layer.alert("请填写使用说明！");
        return false;
    }


    if (kqmc == "") {
        layer.alert("请填写卡券名称！");
        return false;
    }

    if (yh == true) {
        console.log(yh)
        var discount = $("#discount").val();
        if (discount == "") {
            layer.alert("请填写折扣！");
            return false;
        }
        if (!(/(^[1-9]\d*$)/.test(discount))) {
            layer.alert("折扣请填写整数！");
            return false;

        }

        if (parseFloat(discount) < 1 || parseFloat(discount) > 99) {
            layer.alert("折扣最小值为1，最大值为99！");
            return false;
        }
    }
    if (jieshao == true) {
        var conten = $("#conten").val();
        if (conten == "") {
            layer.alert("请优惠券详情！");
            return false;
        }
    }
    if (money == true) {

        var least_cost = $("#least_cost").val();
        var reduce_cost = $("#reduce_cost").val();
        if (least_cost == "") {
            // console.log(123);
            layer.alert("请填写起用金额！");
            return false;
        }
        if (reduce_cost == "") {
            // console.log(231);
            layer.alert("请填写减免金额！");
            return false;
        }
        // console.log(parseFloat(least_cost));
        if (parseFloat(reduce_cost) < 0.01 || parseFloat(reduce_cost) > 9999) {

            layer.alert("减免金额最小值为0.01，最大值为9999！");
            return false;
        }
        if (parseFloat(least_cost) < 0.01 || parseFloat(least_cost) > 9999) {

            layer.alert("减免金额最小值为0.01，最大值为9999！");
            return false;
        }
        if (parseFloat(least_cost) <= parseFloat(reduce_cost)) {
            // console.log(least_cost, reduce_cost);
            layer.alert("减免金额请小于起用金额！");
            return false;
        }

    }

    if (brand_name == "") {
        layer.alert("请填写商家名称！");
        return false;
    }
    if (logo_url == "") {
        layer.alert("请上传logo图片！");
        return false;
    }
    if (service_phone == "") {
        layer.alert("请填写客服电话！");
        return false;
    }
    if ($("#brand_name").val().length > 12) {

        layer.alert("商家名称字数超长！");
        return false;
    }

    $.ajax({

        type: "POST",

        url: getcardusAPI,

        cache: false,

        dataType: "json",

        data: $("#creatcard").serializeArray(),		//表单ID

        success: function (data) {

            //console(data);

            if (data.return_code == "SUCCESS") {
                layer.msg('添加成功');
                setTimeout(function(){
                    window.location.href=data.info.url
                }, 1000);
            } else {
                layer.msg('添加失败');
            }

        }

    })
});